package org.linlinjava.litemall.db.service;

        import java.time.LocalDateTime;
        import java.util.List;
        import java.util.Map;

        import javax.annotation.Resource;

        import com.github.pagehelper.Page;
        import com.github.pagehelper.PageHelper;
        import org.linlinjava.litemall.db.dao.LitemallFormSupplyMapper;
        import org.linlinjava.litemall.db.dao.LitemallWantBuyMapper;
        import org.linlinjava.litemall.db.dao.SupplyMapper;
        import org.linlinjava.litemall.db.domain.*;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;
        import org.springframework.util.StringUtils;

@Service
public class wantBuyService {
    @Resource
    private LitemallWantBuyMapper wantBuyMapper;
    @Autowired
    private LitemallUserService userService;

    public List<LitemallWantBuy> querySelective(Integer page, Integer size,String sort,
                                             String order,   String searchValue) {
        LitemallWantBuyExample example = new LitemallWantBuyExample();
        if(searchValue.isEmpty()){
            example.or().andStatusEqualTo((byte) 1).andDeletedEqualTo((byte) 0);
        }else {
            example.or().andStatusEqualTo((byte) 1).andDeletedEqualTo((byte) 0).andBrandLike("%" + searchValue + "%");
            example.or().andStatusEqualTo((byte) 1).andDeletedEqualTo((byte) 0).andSeriesLike("%" + searchValue + "%");
            example.or().andStatusEqualTo((byte) 1).andDeletedEqualTo((byte) 0).andModelLike("%" + searchValue + "%");
            example.or().andStatusEqualTo((byte) 1).andDeletedEqualTo((byte) 0).andProvinceLike("%" + searchValue + "%");
        }

        example.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, size);


        List<LitemallWantBuy> list = wantBuyMapper.selectByExample(example);
        return list;
    }


    public LitemallWantBuy query(Integer id) {
        LitemallWantBuyExample example = new LitemallWantBuyExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo((byte) 0);
        return wantBuyMapper.selectOneByExample(example);
    }

    public int add(LitemallWantBuy formSupply) {
        formSupply.setAddTime(LocalDateTime.now());
        formSupply.setUpdateTime(LocalDateTime.now());
        formSupply.setStatus((byte) 0);
        formSupply.setDeleted((byte) 0);
        return wantBuyMapper.insertSelective(formSupply);
    }

    public int update(LitemallWantBuy formSupply) {
        formSupply.setUpdateTime(LocalDateTime.now());
        return wantBuyMapper.updateByPrimaryKeySelective(formSupply);
    }


    public Object myList(Integer userId, Integer type, Integer page, Integer limit, String sort, String order,String searchValue) {
        LitemallWantBuyExample example = new LitemallWantBuyExample();
        LitemallUser user = userService.findById(userId);
        if(type!=0){
            example.or().andStatusEqualTo((byte) (type-1)).andShopIdEqualTo(user.getShopId()).andDeletedEqualTo((byte) 0).andBrandLike("%" + searchValue + "%");
            example.or().andStatusEqualTo((byte) (type-1)).andShopIdEqualTo(user.getShopId()).andDeletedEqualTo((byte) 0).andSeriesLike("%" + searchValue + "%");
            example.or().andStatusEqualTo((byte) (type-1)).andShopIdEqualTo(user.getShopId()).andDeletedEqualTo((byte) 0).andModelLike("%" + searchValue + "%");
            example.or().andStatusEqualTo((byte) (type-1)).andShopIdEqualTo(user.getShopId()).andDeletedEqualTo((byte) 0).andProvinceLike("%" + searchValue + "%");
        }else {
            example.or().andShopIdEqualTo(user.getShopId()).andDeletedEqualTo((byte) 0).andBrandLike("%" + searchValue + "%");
            example.or().andShopIdEqualTo(user.getShopId()).andDeletedEqualTo((byte) 0).andSeriesLike("%" + searchValue + "%");
            example.or().andShopIdEqualTo(user.getShopId()).andDeletedEqualTo((byte) 0).andModelLike("%" + searchValue + "%");
            example.or().andShopIdEqualTo(user.getShopId()).andDeletedEqualTo((byte) 0).andProvinceLike("%" + searchValue + "%");
        }
        example.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<LitemallWantBuy> list = wantBuyMapper.selectByExample(example);
        return list;
    }

    public Object delete(Integer userId,Integer id) {
        LitemallWantBuyExample example = new LitemallWantBuyExample();
        LitemallWantBuyExample.Criteria criteria = example.createCriteria();

        example.or().andIdEqualTo(id).andDeletedEqualTo((byte) 0);
        LitemallWantBuy wantBuy = wantBuyMapper.selectOneByExample(example);
        LitemallUser user = userService.findById(userId);
        if(user.getShopId()!=wantBuy.getShopId()){
            return null;
        }
        wantBuy.setStatus((byte) 3);
        return wantBuyMapper.updateByExampleSelective(wantBuy, example);
    }

}
